package com.app.shortvideo.widget

import android.app.Activity
import android.content.Context
import android.content.res.ColorStateList
import android.util.AttributeSet
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.app.library.ext.visible
import com.app.shortvideo.R
import com.app.shortvideo.ui.system.SettingActivity
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.SizeUtils
import kotlin.math.abs


class TitleView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr) {
    private var ivBack: ImageView? = null
    private var ivSet: ImageView? = null
    var ivMore: ImageView? = null
    private var tvTitle: TextView? = null
    private var tvRightText: TextView? = null

    private var totalDy = 0
    private var showBalance = false

    init {
        initComponents(context, attrs, defStyleAttr)
    }

    private fun initComponents(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
        val typeArray =
            context.obtainStyledAttributes(attrs, R.styleable.TitleView, defStyleAttr, defStyleAttr)
        val title = typeArray.getString(R.styleable.TitleView_title)
        val rightText = typeArray.getString(R.styleable.TitleView_rightText)
        val showEdit = typeArray.getBoolean(R.styleable.TitleView_showEdit, false)
        val showSet = typeArray.getBoolean(R.styleable.TitleView_showSet, false)
        val color = typeArray.getColor(R.styleable.TitleView_titleColor, 0)
        showBalance = typeArray.getBoolean(R.styleable.TitleView_showBalance, false)
        typeArray.recycle()
        inflate(context, R.layout.view_title, this)
        setPadding(SizeUtils.dp2px(6f), SizeUtils.dp2px(8f), SizeUtils.dp2px(6f), 0)
        ivBack = findViewById(R.id.ivTitleBack)
        ivSet = findViewById(R.id.ivTitleSet)
        ivMore = findViewById(R.id.ivTitleMore)
        tvTitle = findViewById(R.id.tvTitleText)
        tvRightText = findViewById(R.id.tvTitleRight)
        if (color != 0) {
            ivBack?.imageTintList = ColorStateList.valueOf(color)
            ivSet?.imageTintList = ColorStateList.valueOf(color)
            tvTitle?.setTextColor(color)
        }
        if (title.isNullOrEmpty()) {
            tvTitle?.text = ""
        } else {
            tvTitle?.text = title
        }
        if (rightText.isNullOrEmpty()) {
            tvRightText?.visible(false)
        } else {
            tvRightText?.visible(true)
            tvRightText?.text = rightText
        }
        val activityName = getContext().javaClass.simpleName.replace("Activity", "")
        ivBack?.setOnClickListener{
            val ctx = getContext()
            if (ctx is Activity) {
                ctx.finish()
            }
        }
        ivSet?.setOnClickListener{
            SettingActivity.start(getContext())
        }
    }
    override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams {
        val layoutParams = LayoutParams(context, attrs)
        layoutParams.topMargin = BarUtils.getStatusBarHeight()+SizeUtils.dp2px(8f)
        return layoutParams
    }

    /**
     * 这个是布局相关的属性，最终继承的是ViewGroup.LayoutParams，所以上面我们可以直接进行转换
     * --目的是获取自定义属性以及一些使用常量的自定义
     */
    fun setTitle(title: String, showSet: Boolean = false) {
        tvTitle?.text = title
        ivSet?.visible(showSet)
    }

    fun setTitleAlphaWithScroll(recyclerView: RecyclerView) {
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                totalDy -= dy
                val alpha = abs(totalDy) * 1.0f / SizeUtils.dp2px(60f)
                tvTitle?.alpha = alpha
            }
        })
    }

    fun getBackView(): ImageView? {
        return ivBack
    }
    fun getRightView(): TextView? {
        return tvRightText
    }
    fun setRightViewEnabled(isEnabled:Boolean){
        tvRightText?.isEnabled = isEnabled
        if (isEnabled){
            tvRightText?.setTextColor(ContextCompat.getColor(context,R.color.color_main))
        }else{
            tvRightText?.setTextColor(ContextCompat.getColor(context,R.color.color_text_3))
        }
    }

    fun setOnBackListener(listener: OnClickListener) {
        ivBack?.setOnClickListener(listener)
    }

    fun setOnSetListener(listener: OnClickListener) {
        ivSet?.setOnClickListener(listener)
    }

    fun setOnEditListener(listener: OnClickListener) {
        ivSet?.setOnClickListener(listener)
    }

    fun setOnRightListener(listener: OnClickListener) {
        tvRightText?.setOnClickListener(listener)
    }

}